<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('采购单列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>[[#{purchaseOrderNumber}]]：</label>
                                <input type="text" name="orderNo"/>
                            </li>
                            <li>
                                <label>[[#{logisticsCompany}]]：</label>
                                <input type="text" name="logisticsCompany"/>
                            </li>
                            <li>
                                <label>[[#{logisticsOrderNumber}]]：</label>
                                <input type="text" name="logisticsNumber"/>
                            </li>
                            <li>
                                <label>[[#{purchaser}]]：</label>
                                <input type="text" name="purchaser"/>
                            </li>
                            <li>
                                <label>[[#{purchaseDate}]]：</label>
                                <input type="text" class="time-input" id="beginPurchaseDate" th:placeholder="#{beginTime}" name="params[beginPurchaseDate]"/>
                                <span>-</span>
                                <input type="text" class="time-input" id="endPurchaseDate" th:placeholder="#{endTime}" name="params[endPurchaseDate]" />
                            </li>
                            <li>
                                <label>[[#{purchaseStatus}]]：</label>
                                <select name="status" th:with="type=${@dict.getType('jt_purchase_status')}">
                                    <option value="">[[#{all}]]</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <label>[[#{attributes}]]：</label>
                                <select name="attributes" th:with="type=${@dict.getType('jt_product_attributes')}">
                                    <option value="">[[#{all}]]</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <label>[[#{reviewer}]]：</label>
                                <input type="text" name="reviewer"/>
                            </li>
                            <li>
                                <label>[[#{auditTime}]]：</label>
                                <input type="text" class="time-input" id="beginReviewTime" th:placeholder="#{beginTime}" name="params[beginReviewTime]"/>
                                <span>-</span>
                                <input type="text" class="time-input" id="endReviewTime" th:placeholder="#{endTime}" name="params[endReviewTime]" />
                            </li>
                            <li>
                                <label>[[#{createTime}]]：</label>
                                <input type="text" class="time-input" id="beginCreateTime" th:placeholder="#{beginTime}" name="params[beginCreateTime]"/>
                                <span>-</span>
                                <input type="text" class="time-input" id="endCreateTime" th:placeholder="#{endTime}" name="params[endCreateTime]" />
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;[[#{search}]]</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;[[#{reset}]]</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-info" onclick="bindSupplier()" title="请在收到的手机短信中查找订单号，填写并绑定，只需要绑定一次" shiro:hasPermission="business:user:supplier:bind">
                    <i class="fa fa-magnet"></i> [[#{bindSupplier}]]
                </a>
                <a class="btn btn-success" onclick="inBound()" shiro:hasPermission="business:inbound:add">
                    <i class="fa fa-send"></i> [[#{delivery}]]
                </a>
                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="business:purchase:export">
                    <i class="fa fa-download"></i> [[#{export}]]
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: bootstrap-suggest-js" />
     <th:block th:include="include :: bootstrap-table-fixed-columns-js" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('business:purchase:edit')}]];
        var removeFlag = [[${@permission.hasPermi('business:purchase:remove')}]];
        var statusDatas = [[${@dict.getType('jt_purchase_status')}]];
        var attributesDatas = [[${@dict.getType('jt_product_attributes')}]];
        var prefix = ctx + "business/purchase";

        $(function() {
            var options = {
                url: prefix + "/supplier/list",
                viewUrl: prefix + "/view/{id}",
                exportUrl: prefix + "/export",
                modalName: i18n('purchase'),
                sortName: "createTime",
                sortOrder: "desc",
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [{
                    checkbox: true
                },
                {
                    field: 'purchaseId',
                    title: '采购单id',
                    visible: false
                },
                {
                    field: 'supplierName',
                    title: i18n('supplier')
                },
                {
                    field: 'orderNo',
                    title: i18n('purchaseOrderNumber'),

                    formatter: function (value, row, index) {
                        return '<a href="javascript:void(0)" onclick="$.operate.view(\'' + row.purchaseId + '\')"><b>' + value + '</b></a>';
                    }
                },
                {
                    field: 'attributes',
                    title: i18n('attributes'),
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(attributesDatas, value);
                    }
                },
                {
                    field: 'logisticsCompany',
                    title: i18n('logisticsCompany')
                },
                {
                    field: 'logisticsNumber',
                    title: i18n('logisticsOrderNumber')
                },
                {
                    field: 'totalQty',
                    title: i18n('purchaseQuantity')
                },
                {
                    field: 'totalAmount',
                    title: i18n('purchaseAmount')
                },
                {
                    field: 'purchaser',
                    title: i18n('purchaser')
                },
                {
                    field: 'purchaseDate',
                    title: i18n('purchaseDate'),
                    sortable:true
                },
                {
                    field: 'expectedArrivalDate',
                    title: i18n('expectedDateOfArrival'), //'预计到货日期'
                    sortable: true,
                },
                {
                    field: 'status',
                    title: i18n('status'),
                    formatter: function(value, row, index) {
                       return $.table.selectDictLabel(statusDatas, value);
                    }
                },
                {
                    field: 'reviewer',
                    title: i18n('reviewer')
                },
                {
                    field: 'reviewTime',
                    title: i18n('reviewTime'),
                    sortable:true
                },
                {
                    field: 'receiver',
                    title: i18n('receiver')
                },
                {
                    field: 'phoneNumber',
                    title: i18n('phoneNumber')
                },
                {
                    field: 'fullAddress',
                    title:  i18n('detailedAddress'),
                    formatter: function(value, row, index) {
                        var detailedAddress = "";
                        if(row.country != null){
                            detailedAddress += row.country;
                        }
                        if(row.province != null){
                            detailedAddress += row.province;
                        }
                        if(row.city != null){
                            detailedAddress += row.city;
                        }
                        if(row.district != null){
                            detailedAddress += row.district;
                        }
                        if(row.district != null){
                            detailedAddress += row.fullAddress;
                        }
                        return detailedAddress;
                    }
                },
                {
                    field: 'remark',
                    title: i18n('remark')
                },
                {
                    field: 'createBy',
                    title: i18n('createBy')
                },
                {
                    field: 'createTime',
                    title: i18n('createTime'),
                    sortable:true
                },
                {
                    title: i18n('operate'),
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        if(row.status == 0){
                            actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="send(\'' + row.purchaseId + '\')"><i class="fa fa-edit"></i>发货</a> ');
                        }
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });
    </script>
     <script th:inline="javascript">
         var warehouseSuggest = $("#suggest-warehouse").bsSuggest({
             url: ctx + "business/warehouse/select/list",
             idField: "warehouseId",
             keyField: "warehouseName",
             clearable: true,
             ignorecase: true,
             hideOnSelect: true,
             effectiveFields: ["warehouseId", "warehouseName"],
             effectiveFieldsAlias: {warehouseId: "仓库ID", warehouseName: "仓库"},
         }).on('onSetSelectValue', function (e, selectedData, selectedRawData) {
             console.log('onSetSelectValue: ', selectedData);

             $("input[name='warehouseId']").val(selectedRawData.warehouseId);
         }).on('onUnsetSelectValue', function (e) {
             //console.log("onUnsetSelectValue=="+e);
             $("input[name='warehouseId']").val("");
         });


         function inBound() {
             table.set();
             var rows = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
             if (rows.length == 0) {
                 $.modal.alertWarning(i18n('pleaseSelectAtLeastOne'));
                 return;
             }

             $.modal.confirm("确认【生成入库单】选中的" + rows.length + "条数据吗?", function() {
                 var url = prefix + "/inbound/gen";
                 var data = { "ids": rows.join() };
                 $.operate.submit(url, "post", "json", data);
             });
         }

         function send(purchaseId) {
             $.modal.confirm("确认【生成入库单】选中的" + rows.length + "条数据吗?", function() {
                 var url = prefix + "/inbound/gen";
                 var data = { "ids": purchaseId };
                 $.operate.submit(url, "post", "json", data);
             });
         }

         function bindSupplier() {
             var orderNo = $("input[name='orderNo']").val();
             if(orderNo ==""){
                 $.modal.alertWarning("请填写采购单号");
                 return;
             }
             $.modal.confirm("确认【绑定】供应商吗?", function() {
                 var url = ctx + "business/user/supplier/bind";
                 var data = { "purchaseOrderNo": orderNo };
                 $.operate.submit(url, "post", "json", data);
             });
         }
     </script>
     <script th:inline="javascript">
         var ROOT = [[${#servletContext.contextPath}]];
         var LANG_COUNTRY = [[${#locale.language+'_'+#locale.country}]];

         //初始化i18n
         $.i18n.properties({
             path: ROOT + '/i18n/',//这里表示访问路径
             name: 'messages',//文件名开头
             language: LANG_COUNTRY,//文件名语言 例如en_US
             mode: 'map'//默认值
         });

         //i18n函数
         function i18n(msgKey) {
             try {
                 return $.i18n.prop(msgKey);
             } catch (e) {
                 return msgKey;
             }
         }
     </script>
</body>
</html>